package com.welike.shibo.eneity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.welike.shibo.utils.IpUtil;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * Created by fangyu on 2019-08-24.
 */
@Data
@TableName(value = "log")
public class Log {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 操作人
     */
    @TableField(value = "operation_name")
    private String operationName;

    /**
     * 操作类别
     */
    @TableField(value = "operation_type")
    private String operationType;


    /**
     * 操作动作
     */
    @TableField(value = "operation_action")
    private String operationAction;

    /**
     * 操作状态
     */
    @TableField(value = "operation_state")
    private String operationState;

    /**
     * 操作结果
     */
    @TableField(value = "operation_result")
    private String operationResult;

    /**
     * 操作时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    /**
     * ip地址
     */
    @TableField(value = "ip")
    private String ip;

    public static final String COL_ID = "id";

    public static final String COL_OPERATION_NAME = "operation_name";

    public static final String COL_OPERATION_TYPE = "operation_type";

    public static final String COL_OPERATION_ACTION = "operation_action";

    public static final String COL_OPERATION_STATE = "operation_state";

    public static final String COL_OPERATION_RESULT = "operation_result";

    public static final String COL_CREATE_TIME = "create_time";

    public static final String COL_IP = "ip";

    public Log() {
    }

    public Log(String operationName, String operationAction, Date createTime) {
        this.operationName = operationName;
        this.operationAction = operationAction;
        this.createTime = createTime;
    }

    public Log(String operationName, String operationType, String operationAction,
               String operationState, String operationResult, HttpServletRequest request) throws UnknownHostException {
        this.operationName = operationName;
        this.operationType = operationType;
        this.operationAction = operationAction;
        this.operationState = operationState;
        this.operationResult = operationResult;
        this.createTime = new Date();
        this.ip = IpUtil.getIpCity(request);
    }
}